package com.start.cas;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson2.JSONObject;
import com.start.common.exceptions.StartBusException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class StartLocalCasServiceImpl implements StartClientService {
    @Autowired
    private StartCasClientsConfig casClientsConfig;

    /**
     * 是否存在客户单配置
     *
     * @param appId
     * @return true 存在 false不存在
     */
    @Override
    public boolean hasClient(String appId) {
        return casClientsConfig.hasClient(appId);
    }

    @Override
    public StartCasClient getClientConfig(String appId) {
        return casClientsConfig.getClientConfig(appId);
    }

    /**
     * 校验签名
     *
     * @param appId      appId
     * @param appSecret
     * @param token      用户token
     * @param timeMillis 时间戳
     * @param sing       签名
     * @param props      扩展参数
     * @return true 签名正确
     */
    @Override
    public boolean checkSignature(String appId, String appSecret, String token, String timeMillis, String sing, JSONObject props) {
        StartCasClient clientConfig = getClientConfig(appId);
        String data = appId + timeMillis + token + appSecret + (StringUtils.isAllBlank(clientConfig.getSalt()) ? "" : clientConfig.getSalt());
        if (SecureUtil.md5(data).equals(sing)) {
            throw new StartBusException("signature error");
        }
        return true;
    }
}
